package com.kapcb.mybatis.plus.handler;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.kapcb.mybatis.plus.interceptor.MyBatisProperties;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * <a>Title: MyBatisPlusMetaObjectHandler </a>
 * <a>Author: kapcb <a>
 * <a>Description: MyBatisPlusMetaObjectHandler <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/6/14 20:47
 * @since 1.0
 */
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    private final MyBatisProperties myBatisProperties;

    public MyBatisPlusMetaObjectHandler(MyBatisProperties myBatisProperties) {
        this.myBatisProperties = myBatisProperties;
    }

    /**
     * 使用 MyBatis Plus 的 ORM 方式插入表对象时自动填充实体对象中的 CreateTime、CreateUser 字段
     *
     * @param metaObject {@link MetaObject}
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        String autoFillCreateTimeFieldName = myBatisProperties.getAutoFillCreateTimeFieldName();
        if (StringUtils.isNotBlank(autoFillCreateTimeFieldName) && this.getFieldValByName(autoFillCreateTimeFieldName, metaObject) == null) {
            this.strictInsertFill(metaObject, autoFillCreateTimeFieldName, LocalDateTime.class, LocalDateTime.now());
        }

        String autoFillCreateUserFieldName = myBatisProperties.getAutoFillCreateUserFieldName();
        if (StringUtils.isNotBlank(autoFillCreateUserFieldName) && this.getFieldValByName(autoFillCreateUserFieldName, metaObject) == null) {
            String createUserName = "test create user name";
            this.strictInsertFill(metaObject, autoFillCreateUserFieldName, String.class, createUserName);
        }

        String autoFillCreateUserIdFiledName = myBatisProperties.getAutoFillCreateUserIdFiledName();
        if (StringUtils.isNotBlank(autoFillCreateUserIdFiledName) && this.getFieldValByName(autoFillCreateUserIdFiledName, metaObject) == null) {
            this.strictInsertFill(metaObject, autoFillCreateUserIdFiledName, Long.class, Optional.of(StpUtil.getLoginIdAsLong()).orElse(null));
        }
    }

    /**
     * 使用 MyBatis Plus 的 ORM 方式插入表对象时自动填充实体对象中的 UpdateTime、UpdateUser 字段
     *
     * @param metaObject {@link MetaObject}
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        String autoFillUpdateTimeFieldName = myBatisProperties.getAutoFillUpdateTimeFieldName();
        if (StringUtils.isNotBlank(autoFillUpdateTimeFieldName) && this.getFieldValByName(autoFillUpdateTimeFieldName, metaObject) == null) {
            this.strictInsertFill(metaObject, autoFillUpdateTimeFieldName, LocalDateTime.class, LocalDateTime.now());
        }

        String autoFillUpdateUserFieldName = myBatisProperties.getAutoFillUpdateUserFieldName();
        if (StringUtils.isNotBlank(autoFillUpdateUserFieldName) && this.getFieldValByName(autoFillUpdateUserFieldName, metaObject) == null) {
            String updateUserName = "test update user name";
            this.strictInsertFill(metaObject, autoFillUpdateUserFieldName, String.class, updateUserName);
        }

        String autoFillUpdateUserIdFiledName = myBatisProperties.getAutoFillUpdateUserIdFiledName();
        if (StringUtils.isNotBlank(autoFillUpdateUserIdFiledName) && this.getFieldValByName(autoFillUpdateUserIdFiledName, metaObject) == null) {
            this.strictInsertFill(metaObject, autoFillUpdateUserIdFiledName, Long.class, Optional.of(StpUtil.getLoginIdAsLong()).orElse(null));
        }
    }

}
